﻿using System.Web.Mvc;

using Freetour.UI.Models.Membership.Account;
using Freetour.Application.Services;
using Freetour.Application.Models.Membership;
using System.Web;

namespace Freetour.UI.Controllers.Membership
{
    public class MembershipController : Controller
    {
        #region Login

        public ActionResult Login()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult Login(LoginOnModel model)
        {
            if (ServicesFactory.Instance.Membership.Login(model.UserID, model.Password))
            {
                if (model.AutoLogin)
                {
                    var cookie = new HttpCookie("membership");
                    cookie["name"] = model.UserID;
                    cookie["password"] = model.Password;
                    cookie.Secure = true;
                    Response.Cookies.Add(cookie);
                }

                Session.Clear();
                Session["user"] = model.UserID;

                return RedirectToAction("index", "Home");
            }
            else
            {
                return RedirectToAction("login");
            }
        }

        public ActionResult LoginFailed()
        {
        }

        #endregion

        #region Logout

        // Get: /User/Logout
        public ActionResult Logout()
        {
            return View();
        }

        #endregion

        #region Register
        public ActionResult Register()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(RegisterModel model)
        { 
            var membershipEntity = new MembershipEntity()
            {
                UserName = model.UserID,
                Password = model.Password
            };
            try
            {
                ServicesFactory.Instance.Membership.Register(membershipEntity);

                ViewData["register"] = "注册成功！";
            }
            catch
            {
            }
            
            return View("EditPersonalInfo");
        }

        #endregion
    }
}
